/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.ly.wxstore.web;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.wxstore.entity.goods.Goods;
import com.ly.wxstore.service.account.ShiroDbRealm.ShiroUser;
import com.ly.wxstore.service.goods.GoodsService;
import com.ly.wxstore.service.weixin.WeixinConf;

/**
 * 商城controller
 * 
 * @author peter
 */
@Controller
@RequestMapping(value = "/Store")
public class StoreController {

	private static Logger logger = LoggerFactory.getLogger(StoreController.class);
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private WeixinConf weixinConf;

	
	/**
	 * 商品列表接口
	 * @return
	 */
	@RequestMapping(value="goodslist.json",method = RequestMethod.GET)
	@ResponseBody
	public List<Goods> list() {
		logger.info("Store/list");
		List<Goods> list = goodsService.getByWeixinPublicId(weixinConf.getPublicId());
		
		return list;
	}
	
	/**
	 * 商品详情
	 * @return
	 */
	@RequiresRoles("user")
	@RequestMapping(value="goodsDetail/{goodsId}.json",method = RequestMethod.GET)
	@ResponseBody
	public Goods goodsDetail(@PathVariable("goodsId") Long goodsId) {
		logger.info("goodsDetail："+goodsId);
		
		return goodsService.getById(goodsId);
	}
	
	@RequestMapping(method = RequestMethod.GET)
    public String getStorePage() {
		logger.info("Store");
        return "Store";
    }
	
	@RequestMapping(value="GoodsDetail",method = RequestMethod.GET)
    public String getGoodsDetailPage() {
		logger.info("GoodsDetail");
        return "GoodsDetail";
    }
	
	public String getCurrentUserSid() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.getSid();
	}

}
